package c2.cross.business.championrace.sub;

import c2.cross.business.base.CrossSubModule;
import c2.cross.business.championrace.ChampionRaceModule;
import c2.cross.business.ladder.common.LadderOppoDetail;
import c2.cross.business.ladder.rank.LadderRankDo;
import cate.common.table.d.GDChampionRace;
import cate.game.pvp.championrace.data.ChampionRacePs;
import cate.game.pvp.ladder.common.LadderRankListQuery;
import cate.game.pvp.ladder.common.LadderRankListResult;
import cate.game.pvp.ladder.data.LadderRank;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ChampionRacePullDataModule extends CrossSubModule<ChampionRaceModule> {
    public List<ChampionRacePs> pull() {
        log.info("尝试从天梯获取前256名玩家");
        LadderRankListQuery query = new LadderRankListQuery();
        query.limit = 256;
        LadderRankListResult result = new LadderRankDo().getRank(parent.getLadderModule().rank, query);
        List<ChampionRacePs> psList = new ArrayList<>();
        if (result != null && CollectionUtils.isNotEmpty(result.list)) {
            List<LadderRank> ladderRanks = result.list;
            for (int i = 0; i < ladderRanks.size(); ++i){
                LadderRank rank = ladderRanks.get(i);
                LadderOppoDetail detail = parent.getLadderModule().rank.getOppoDetail(rank.owner.uid);
                if (detail != null && !detail.robot) {
                    psList.add(ChampionRacePs.of(detail.rolePs, GDChampionRace.getLadderTransPoint(i + 1), i + 1));
                }
            }
        }
        log.info("最终从天梯获取了{}名玩家", psList.size());
        return psList;
    }
}
